package com.ruoyi.web.controller.core;

import java.util.List;
import java.util.Optional;

import com.ruoyi.core.domain.Codes;
import com.ruoyi.core.domain.Shop;
import com.ruoyi.core.domain.vo.ShopUserVo;
import com.ruoyi.core.service.IShopService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.core.domain.ShopUser;
import com.ruoyi.core.service.IShopUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网点员工列表Controller
 * 
 * @author lgc
 * @date 2020-08-08
 */
@Controller
@RequestMapping("/core/shopUser")
public class ShopUserController extends BaseController
{
    private String prefix = "core/shopUser";

    @Autowired
    private IShopUserService shopUserService;
    @Autowired
    private IShopService shopService;
    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("core:shopUser:view")
    @GetMapping()
    public String shopUser(Model model)
    {
        model.addAttribute("shopList", shopService.selectShopAll(ShiroUtils.getUserId()));
        return prefix + "/shopUser";
    }

    /**
     * 检测号码是否重复
     */
    @PostMapping( "/testPhone")
    @ResponseBody
    public Boolean  testPhone(String phone){
        SysUser user = sysUserService.selectUserByPhoneNumber(phone);
        //该对象不存在返回false
        if (Optional.ofNullable(user).isPresent()){
            return false;
        }
        return true;
    }

    /**
     * 查询网点员工列表列表
     */
    @RequiresPermissions("core:shopUser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShopUser shopUser)
    {
        Long userId = ShiroUtils.getUserId();
        shopUser.setCreatedBy(userId);
        startPage();
        List<ShopUserVo> list = shopUserService.selectShopUserVoList(shopUser);
        return getDataTable(list);
    }

    /**
     * 导出网点员工列表列表
     */
    @RequiresPermissions("core:shopUser:export")
    @Log(title = "网点员工列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShopUser shopUser)
    {
        List<ShopUser> list = shopUserService.selectShopUserList(shopUser);
        ExcelUtil<ShopUser> util = new ExcelUtil<ShopUser>(ShopUser.class);
        return util.exportExcel(list, "shopUser");
    }

    /**
     * 新增网点员工列表
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("shopList",shopService.selectShopAll(ShiroUtils.getUserId()));
        return prefix + "/add";
    }

    /**
     * 新增保存网点员工列表
     */
    @RequiresPermissions("core:shopUser:add")
    @Log(title = "网点员工列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(String userName, String phonenumber, Long shopId)
    {
        //添加员工 登录账号为手机号码 密码后6位
        Long userId =  sysUserService.insertUserBy(userName,phonenumber,ShiroUtils.getUserId());
        return toAjax(shopUserService.insertShopUser(userId,shopId,ShiroUtils.getUserId()));
    }

    /**
     * 修改网点员工列表
     */
    @GetMapping("/edit/{shopUserId}")
    public String edit(@PathVariable("shopUserId") Long shopUserId, ModelMap mmap)
    {
        ShopUser shopUser = shopUserService.selectShopUserById(shopUserId);
        mmap.put("shopUser", shopUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存网点员工列表
     */
    @RequiresPermissions("core:shopUser:edit")
    @Log(title = "网点员工列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShopUser shopUser)
    {
        return toAjax(shopUserService.updateShopUser(shopUser));
    }

    /**
     * 删除网点员工列表
     */
    @RequiresPermissions("core:shopUser:remove")
    @Log(title = "网点员工列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopUserService.deleteShopUserByIds(ids));
    }
}
